package com.yy.permissions_general.module.system.user.entity;

import com.baomidou.mybatisplus.annotation.*;
import com.baomidou.mybatisplus.extension.activerecord.Model;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.yy.permissions_general.common.constant.SystemConstant;
import com.yy.permissions_general.module.system.menu.vo.MenuVo;
import lombok.Data;
import org.springframework.security.core.CredentialsContainer;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.io.Serializable;
import java.util.Collection;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 用户
 * </p>
 *
 * @author YY
 * @since 2021-10-10
 */
@Data
@TableName("sys_user")
public class User extends Model<User> implements UserDetails, CredentialsContainer {

    private static final long serialVersionUID = 1L;

    /**
     * 主键
     */
    @TableId(value = "id", type = IdType.AUTO)
    private Integer id;

    /**
     * 头像
     */
    @TableField("head_img")
    private String headImg;

    /**
     * 用户名
     */
    @TableField("username")
    private String username;

    /**
     * 生日
     */
    @TableField("birthday")
    @JsonFormat(pattern = SystemConstant.DATE_FORMAT)
    private Date birthday;

    /**
     * 性别
     */
    @TableField("sex")
    private String sex;

    /**
     * 邮箱
     */
    @TableField("email")
    private String email;

    /**
     * 手机号
     */
    @TableField("phone")
    private String phone;

    /**
     * 账号
     */
    @TableField("account")
    private String account;

    /**
     * 密码
     */
    @TableField("password")
    private String password;

    /**
     * 状态:0正常;1锁定
     */
    @TableField("status")
    private Integer status;

    /**
     * 个人简介
     */
    @TableField("personal_profile")
    private String personalProfile;

    /**
     * 创建时间
     */
    @TableField(value = "create_time", fill = FieldFill.INSERT)
    @JsonFormat(pattern = SystemConstant.TIME_FORMAT)
    private Date createTime;

    /**
     * 修改时间
     */
    @TableField(value = "update_time", fill = FieldFill.UPDATE)
    @JsonFormat(pattern = SystemConstant.TIME_FORMAT)
    private Date updateTime;

    /**
     * 最后一次登录时间
     */
    @TableField(value = "last_time")
    @JsonFormat(pattern = SystemConstant.TIME_FORMAT)
    private Date lastTime;

    /**
     * 0:正常;1:删除
     */
    @TableField("is_delete")
    private Integer isDelete;

    /*角色ID*/
    @TableField(exist = false)//表示该属性不为数据库表字段，但又是必须使用的。
    private String roleIds;

    /*角色名称*/
    @TableField(exist = false)
    private String roleNames;

    /*权限类型0目录1菜单2按钮*/
    @TableField(exist = false)
    private String type;

    /**
     * 权限
     */
    @TableField(exist = false)
    private List<MenuVo> menuVoList;

    /**
     * 是否过期
     *
     * @return
     */
    @Override
    public boolean isAccountNonExpired() {
        return true;
    }


    /**
     * 是否锁定
     *
     * @return
     */
    @Override
    public boolean isAccountNonLocked() {
        return 0 == this.getStatus();
    }


    /**
     * 凭证是否过期
     *
     * @return
     */
    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    /**
     * 是否可用
     *
     * @return
     */
    @Override
    public boolean isEnabled() {
        return 0 == this.getStatus();
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return null;
    }

    @Override
    public void eraseCredentials(){
    }

    @Override
    protected Serializable pkVal() {
        return this.id;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj instanceof User) {
            return this.username.equals(((User) obj).username);
        }
        return false;
    }

    @Override
    public int hashCode() {
        return this.username.hashCode();
    }
}
